package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JDialog;
import main.AppConstants;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * TieDialog is shown when a match as ended without a winner.
 */
public class TieDialog extends JDialog {

    private final ImageIcon space = new ImageIcon(getClass().getClassLoader().getResource(AppConstants.IMAGE_TIE));
    public static int delay = 25; // decrease for faster speed on ship + laser
    private final int shipSpeed = 5; // increase for faster ship speed
    private final int laserSpeed = 10; // increase for faster laser speed
    
    public TieDialog() {
	setTitle(AppConstants.DRAWN_DIALOG_TITLE);
	this.setLayout(new BorderLayout());
	

	/* add the different panels on to the JDialog */
	this.add(createUpperJPanel(), BorderLayout.NORTH); 
	this.add(createDrawPanel(), BorderLayout.CENTER);
	this.add(createLowerJPanel(), BorderLayout.SOUTH);

	setModal(true);
	setSize(500, 500);
	setLocationRelativeTo(null);
	setVisible(true);
    }
    
    /*create an DrawPanel, with the desired settings*/
    private JPanel createDrawPanel(){
	JPanel jpanel = new DrawJPanel();
	jpanel.setVisible(true);
	jpanel.setSize(500, 500);
	jpanel.setBackground(Color.BLACK);
	return jpanel;
    }
    
    /* create an JPanel that contains what one wants to be shown on the top of the dialog*/
    private JPanel createUpperJPanel(){
	
	JPanel upperJpanel = new JPanel();
	upperJpanel.setLayout(new GridLayout(1, 0));
	upperJpanel.setBackground(Color.BLACK);
	
	JLabel jlabel2 = new JLabel("TIE");
	jlabel2.setFont(new Font("serif", Font.BOLD, 30));
	jlabel2.setForeground(Color.YELLOW);
	jlabel2.setHorizontalAlignment(SwingConstants.CENTER);
	upperJpanel.add(jlabel2); // add "TIE" at the top of the screen
	
	return upperJpanel;
    }
    
    /* create an JPanel that contains what you want to be shown on the bottom of the dialog*/
    private JPanel createLowerJPanel(){
	JPanel lowerJpanel = new JPanel();
	lowerJpanel.setBackground(Color.BLACK);
	
	JButton jBtnOK = new JButton("OK");
	jBtnOK.setSize(50, 50);
	jBtnOK.setBackground(Color.BLACK);
	jBtnOK.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent ae) {
		dispose(); // dispose of JDialog on button press
	    }
	});
	
	
	lowerJpanel.add(jBtnOK); // add "OK" button to lowerJpanel
	return lowerJpanel;
    }
    
    /*create a JPanel that shows a tiefighter flying by shooting*/
    public class DrawJPanel extends JPanel {
	private Polygon polygon; //  = new Polygon();
	private int drawX = getWidth();
	private int drawY = getHeight() / 2;
	private int drawLaser = drawX - 15; // x cordinate for laser
	
	
	
	Timer timer = new Timer(delay, new TimerListener()); // set an timer to perform the actionPerformed method
	
	public DrawJPanel() {
	    super();
	    this.setLayout(new GridLayout(0, 2, 10, 10));
	    this.setSize(500, 500);
	    timer.start();  // start timer
	}
	
	/* create an tiefighter as an polygon */
	private void setTieAtXY(int x, int y){
	    polygon = new Polygon();
	    polygon.addPoint(25+x, 25+y); // upper left corner
	    polygon.addPoint(75+x, 25+y); // upper right corner
	    polygon.addPoint(95+x, 45+y); // right corner up
	    polygon.addPoint(5+x, 45+y); // draw line from right corner up to left corner up
	    polygon.addPoint(95+x, 45+y); // draw the line back
	    
	    
	    polygon.addPoint(95+x, 80+y); // right corner down
	    polygon.addPoint(5+x, 80+y); // draw line from right corner down to left corner down
	    polygon.addPoint(95+x, 80+y); // draw the line back
	    polygon.addPoint(75+x, 100+y); // lower right corner
	    polygon.addPoint(25+x, 100+y); // lower left corner // lÃƒÂ¤gg lowerleft hÃƒÂ¤r
	    polygon.addPoint(5+x, 80+y); // left corner down
	    polygon.addPoint(5+x, 45+y); // left corner up
	    
	    
	}
	
	
	/* draws the tie fighter, the laser and the background picture */
	@Override
	protected void paintComponent(Graphics g) {
	    super.paintComponent(g); // kommentera bort senare
	    
	    
	    
	    g.drawImage(space.getImage(), 0, 0, getWidth(), getHeight(), this); // background
	    
	    if(drawX <= -100){
		drawX = getWidth() + 100; // -100 +100 to start and stop the animation outside the visible screen
		drawY = getHeight() / 2 - 100;
		drawLaser = drawX - 10;
	    }
	    if(drawLaser <= -15){
		drawLaser = drawX - 10;
	    }
	    drawX -= shipSpeed;
	    // draw tie fighter
	    g.setColor(Color.BLACK);
	    setTieAtXY(drawX, drawY);
	    g.fillPolygon(polygon);
	    g.setColor(Color.GRAY);
	    g.drawPolygon(polygon);
	    g.fillRect(5+drawX, 45+drawY, 90, 35); // (upperleftX, upperleftY, (upperRightX - upperLeftX), (lowerRightY - upperRightY))
	    
	    // draw laser
	    g.setColor(Color.RED);
	    g.drawLine(drawLaser-15, drawY+75, drawLaser, drawY+75);
	    
	    drawLaser -= laserSpeed;
	}
	
	private class TimerListener implements ActionListener{

	    @Override
	    public void actionPerformed(ActionEvent e) {
		repaint();
	    }
	    
	}

    }
}